package ssch.fhnw.firsttest;

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
import javax.swing.*;

import java.sql.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFileChooser;

import babel.SimpleAnalyzer;



public class BabelGui extends JFrame {
	
	BorderLayout MainBorder = new BorderLayout();
	FlowLayout flow = new FlowLayout();
	JButton ButtonOne = new JButton("Browse");
	JButton ButtonTwo = new JButton("Analyse");
	JTextArea Input = new JTextArea(9,30);
	JScrollPane inputscrolling = new JScrollPane(Input);
	JTextArea Output = new JTextArea();
	JScrollPane outputscrolling = new JScrollPane(Output);
	JTextField FileName = new JTextField();
	
	private JFileChooser fileChooser = new JFileChooser();
	
	
	public BabelGui (){
		super ("Babel");
		
		this.getContentPane().setLayout(MainBorder);
		
		this.add(ButtonOne, BorderLayout.NORTH);
		this.add(inputscrolling, BorderLayout.WEST);
		this.add(outputscrolling, BorderLayout.CENTER);
		this.add(ButtonTwo, BorderLayout.EAST);
		this.add(FileName, BorderLayout.SOUTH);
		
		this.Input.setText("Input Text");
		this.Output.setText("Output Text");
		addEventHandlers();

		this.setSize(800,600);
		this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		this.setVisible(true);
	}
	
	private void addEventHandlers() {
		// The file-browser
		ButtonOne.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent evt) {
				chooseInputFile();
			}
		});
		
		ButtonTwo.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent evt) {
				Analyse();
			}
		});

		
	}
	
	private void chooseInputFile() {
		int result = fileChooser.showOpenDialog(this);

	    if (result == JFileChooser.APPROVE_OPTION) {
	        File file = fileChooser.getSelectedFile();
	        FileName.setText(file.getAbsolutePath());
	    	}	
	    
	   input();
    }

	private void Analyse(){
		String FileName = this.FileName.getText();
		File inputFile = checkInputFile(FileName);
		
		if (inputFile != null) {
			SimpleAnalyzer analyzer = new SimpleAnalyzer(inputFile, 3);
			try {
				analyzer.analyze();
			} catch (IOException e) {
				JOptionPane.showMessageDialog(this, e.toString(), "Error during analysis", JOptionPane.ERROR_MESSAGE);
			}
			
			analyzer.printResults();
		}
	}
	
	private File checkInputFile(String fileName) {
		boolean allOk = false;
		File fileIn = new File(fileName);
		
		if (!fileIn.exists()) {
			JOptionPane.showMessageDialog(this, "File does not exist", "File error", JOptionPane.ERROR_MESSAGE);
		} else if (!fileIn.isFile()) {
			JOptionPane.showMessageDialog(this, "Must be a file, not a directory", "File error", JOptionPane.ERROR_MESSAGE);
		} else if (!fileIn.canRead()) {
			JOptionPane.showMessageDialog(this, "File is not readable", "File error", JOptionPane.ERROR_MESSAGE);
		} else {
			allOk = true;
		}
		
		if (!allOk) fileIn = null;
		
		return fileIn;
		}
		
	public void input(){
		try {
			BufferedReader r = new BufferedReader(new FileReader(FileName.getText()));
			
			String line;
			while((line = r.readLine()) != null){
			this.Input.setText(this.Input.getText()+" "+line+ "\n");
			}
			r.close();
		}
		catch (Exception e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
	
	}
}
